//
//  FilterRegionsViewController.m
//  Fangmm
//
//  Created by 阮琦 on 16/3/1.
//  Copyright © 2016年 com.fangmm. All rights reserved.
//

#import "FilterRegionsViewController.h"

#import "ACMacros.h"
#import "Utility.h"
#import "Constants.h"

#import <MobClick.h>

@interface FilterRegionsViewController () <UITableViewDataSource, UITableViewDelegate>

@end

@implementation FilterRegionsViewController {
    id area;
    NSMutableArray *district;
    NSMutableArray *region;
    NSInteger component;
    NSInteger selectedRow;
    NSString *regionCode;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self insertBackground];
    [self configureTableView];
    [self setRegionCode];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated {
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    [MobClick beginLogPageView:@"地区筛选"];
//    self.navigationController.navigationBarHidden = true;
}

- (void)viewWillDisappear:(BOOL)animated {
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
    [MobClick endLogPageView:@"地区筛选"];
//    self.navigationController.navigationBarHidden = false;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark - Configuring the view’s layout behavior
- (void)insertBackground {
    UIImageView *backgroundImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, Main_Screen_Width, Main_Screen_Height)];
    [backgroundImageView setImage:[UIImage imageNamed:@"screen_bg"]];
    [self.view insertSubview:backgroundImageView atIndex:0];
}

- (void)configureTableView {
    self.districtTableView.delegate = self;
    self.districtTableView.dataSource = self;
    self.businessTableView.delegate = self;
    self.businessTableView.dataSource = self;
    self.businessTableView.hidden = true;
    [self.districtTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self.businessTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    area = [Utility getObjectForKey:kOpenArea];
    district = [area objectForKey:@"subs"];
    component = 0;
    selectedRow = 0;
}

- (void)setRegionCode {
    regionCode = [NSString stringWithFormat:@"%@",[area objectForKey:@"code"]];
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if ([tableView isEqual:self.districtTableView]) {
        return district.count + 1;
    } else {
        [self resetRegionWithComponent:component];
        return region.count + 1;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger row = indexPath.row;
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    [cell setBackgroundColor:[UIColor clearColor]];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    [cell.textLabel setFont:[UIFont systemFontOfSize:15]];
    [cell.textLabel setTextColor:[UIColor whiteColor]];
    [cell setTintColor:[UIColor groupTableViewBackgroundColor]];
    if ([tableView isEqual:self.districtTableView]) {
        switch (row) {
            case 0:
                [cell.textLabel setText:@"不限"];
                break;
            default:
                [cell.textLabel setText:[district[row - 1] objectForKey:@"name"]];
                break;
        }
        if (component == row) {
            [cell.textLabel setTextColor:RGBACOLOR(255, 118, 108, 1)];
        }
    } else {
        switch (row) {
            case 0:
                [cell.textLabel setText:@"全部"];
                break;
                
            default:
                [cell.textLabel setText:[region[row - 1] objectForKey:@"name"]];
                break;
        }
        if (selectedRow == row) {
            [cell.textLabel setTextColor:RGBACOLOR(255, 118, 108, 1)];
            [cell setAccessoryType: UITableViewCellAccessoryCheckmark];
        }
    }
    return cell;
}

#pragma mark - Table view delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([tableView isEqual:self.districtTableView]) {
        [[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:component inSection:0]].textLabel setTextColor:[UIColor whiteColor]];
        component = indexPath.row;
        selectedRow = 0;
        [[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:component inSection:0]].textLabel setTextColor:RGBACOLOR(255, 118, 108, 1)];
        if (component == 0) {
            self.businessTableView.hidden = true;
        } else {
            self.businessTableView.hidden = false;
            [self.businessTableView reloadData];
        }
    } else {
        [[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:selectedRow inSection:0]].textLabel setTextColor:[UIColor whiteColor]];
        [[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:selectedRow inSection:0]] setAccessoryType:UITableViewCellAccessoryNone];
        selectedRow = indexPath.row;
        [[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:selectedRow inSection:0]].textLabel setTextColor:RGBACOLOR(255, 118, 108, 1)];
        [[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:selectedRow inSection:0]] setAccessoryType:UITableViewCellAccessoryCheckmark];
    }
}

#pragma mark - Triggering Methods
- (IBAction)clickToDoneFilter:(UIButton *)sender {
    [self filterWithComponent:component WithRow:selectedRow];
    [self dismissViewControllerAnimated:true completion:^{
    }];
}

- (IBAction)clickToCancelBack:(UIButton *)sender {
    [self dismissViewControllerAnimated:true completion:^{
        
    }];
}

- (void)resetRegionWithComponent:(NSInteger)selectedComponent {
    component = selectedComponent;
    if (component == 0) {
    } else {
        region = [district[component - 1] objectForKey:@"subs"];
    }
}

- (void)filterWithComponent:(NSInteger)Selectedcomponent WithRow:(NSInteger)row {
    if (component == 0) {
        regionCode = [NSString stringWithFormat:@"%@",[area objectForKey:@"code"]];
        if (regionCode == nil) {
            regionCode = @"3502";
        }
        [self.delegate doneFilterWithDistrict:@"不限" WithRegion:@"" WithRegionCode:regionCode];
    } else {
        NSString *districtName = [district[component - 1] objectForKey:@"name"];
        NSString *regionName = @"";
        if (row == 0) {
            regionName = @"全部";
            regionCode = [NSString stringWithFormat:@"%@",[district[component - 1] objectForKey:@"code"]];
        } else {
            regionName = [region[row - 1] objectForKey:@"name"];
            regionCode = [NSString stringWithFormat:@"%@",[region[row - 1] objectForKey:@"code"]];
        }
        [self.delegate doneFilterWithDistrict:districtName WithRegion:regionName WithRegionCode:regionCode];
    }
}
@end
